//
//  MLSimpleWebViewController.m
//
//  Created by molon on 9/3/14.
//  Copyright (c) 2014 molon. All rights reserved.
//

#import "MLSimpleWebViewController.h"
#import "UIViewController+MLKit.h"

@interface MLSimpleWebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation MLSimpleWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    if (self.loadURL) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:self.loadURL]];
    }
    
    NSArray *items = @[[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)],
                       [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(disappear)]
                       ];
    self.navigationItem.leftBarButtonItems = items;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
}

- (BOOL)hidesBottomBarWhenPushed {
    if (!self.navigationController) {
        return NO;
    }
    
    UIViewController *vc = self;
    while (![self.navigationController isEqual:vc.parentViewController]) {
        vc = vc.parentViewController;
    }
    
    return [[self.navigationController.viewControllers firstObject] isEqual:vc]?NO:[self.navigationController.topViewController isEqual:vc];
}

#pragma mark - getter
- (UIWebView *)webView
{
	if (!_webView) {
		_webView = [UIWebView new];
		_webView.delegate = self;
        _webView.clipsToBounds = YES;
	}
	return _webView;
}

#pragma mark - event
- (void)goBack
{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        [self disappear];
    }
}

#pragma mark - layout
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.webView.frame = self.view.bounds;
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicator startAnimating];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:indicator];
    
    
    self.title = @"加载中...";
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
    
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
    
    if ([error.userInfo[NSURLErrorFailingURLStringErrorKey] isEqualToString:[self.loadURL absoluteString]]) {
        
        self.title = @"加载网页失败,请重试";
    }
}

#pragma mark - event
- (void)refresh
{
//    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self.webView reload];
//    [self.webView loadRequest:self.webView.request];
}
@end
